﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public enum E_UI_Layer
{
    Bot,
    Mid,
    Top,
    System,
}

public class UIMgr : BaseManager<UIMgr>
{
    //里氏转换原则，用BasePanel基类去装其他子类面板
    private Dictionary<string, BasePanel> panelDic = new Dictionary<string, BasePanel>();

    private Transform bot;
    private Transform mid;
    private Transform top;
    private Transform system;
    //记录Canvas，方便外部使用
    public RectTransform canvas;



    public UIMgr()
    {
        //创建并找到Canvas
        GameObject obj = ResourceMgr.GetInstance().Load<GameObject>("UI/Canvas");
         canvas = obj.transform as RectTransform;
        GameObject.DontDestroyOnLoad(obj);
        //找到Canvas下各层
        bot = canvas.Find("Bot");
        mid = canvas.Find("Mid");
        top = canvas.Find("Top");
        system = canvas.Find("System");

        //创建EventSystem
        obj = ResourceMgr.GetInstance().Load<GameObject>("UI/EventSystem");
        GameObject.DontDestroyOnLoad(obj);
    }

    /// <summary>
    /// 通过枚举得到层级的父对象
    /// </summary>
    /// <param name="layer"></param>
    /// <returns></returns>
    public Transform GetLayerFather(E_UI_Layer layer)
    {
        switch (layer)
        {
            case E_UI_Layer.Bot:
                return this.bot;
                
            case E_UI_Layer.Mid:
                return this.mid;
                
            case E_UI_Layer.Top:
                return this.top;
                
            case E_UI_Layer.System:
                return this.system;
                
        }
        return null;
    }

    /// <summary>
    /// 显示面板
    /// </summary>
    /// <typeparam name="T">面板脚本类型</typeparam>
    /// <param name="panelName">面板名称</param>
    /// <param name="layer">面板层级</param>
    /// <param name="callBack">当面板预设创建成功后，想做的事</param>
    public void ShowPanel<T>(string panelName, E_UI_Layer layer = E_UI_Layer.Mid, UnityAction<T> callBack = null) where T : BasePanel
    {
        ResourceMgr.GetInstance().LoadAsync<GameObject>("UI/" + panelName, (obj) =>
        {
            if (panelDic.ContainsKey(panelName))
            {
                panelDic[panelName].ShowMe();
                if (callBack!= null)
                {
                    callBack(panelDic[panelName] as T);
                }
                //避免面板重复显示，如果存在该面板就直接显示，并调用回调函数后，返回，不执行后面的异步加载逻辑了
                return;
            }
            //要把它作为Canvas的子对象，并且要设置它的相对位置
            Transform father = bot;
            switch (layer)
            {
                case E_UI_Layer.Mid:
                    father = mid;
                    break;
                case E_UI_Layer.Top:
                    father = top;
                    break;
                case E_UI_Layer.System:
                    father = system;
                    break;
                default:
                    break;
            }
            //设置父对象和自己的相对位置和大小
            obj.transform.SetParent(father);
            obj.transform.localPosition = Vector3.zero;
            obj.transform.localScale = Vector3.one;
            (obj.transform as RectTransform).anchorMax = Vector2.zero;
            (obj.transform as RectTransform).anchorMin = Vector2.zero;
            //得到加载的预设体身上的面板脚本
            T panel = obj.GetComponent<T>();
            //处理面板加载完后的逻辑
            if (callBack!=null)
            {
                callBack(panel);
            }
            panel.ShowMe();
            //面板存起来
            panelDic.Add(panelName,panel);
        });
    }
    /// <summary>
    /// 隐藏面板
    /// </summary>
    /// <param name="panelName"></param>
    public void HidePanel(string panelName)
    {
        if (panelDic.ContainsKey(panelName))
        {
            panelDic[panelName].HideMe();
            GameObject.Destroy(panelDic[panelName].gameObject);
            panelDic.Remove(panelName);
        }
    }
    /// <summary>
    /// 获得某一个已经显示的面板
    /// </summary>
    /// <typeparam name="T">面板类型</typeparam>
    /// <param name="panelName">面板名称</param>
    /// <returns></returns>
    public T GetPanel<T>(string panelName) where T:BasePanel
    {
        if (panelDic.ContainsKey(panelName))
        {
            return (panelDic[panelName] as T);
        }
        else 
        {
            return null;
        }
    }

    public static void AddCostomEventListener(UIBehaviour control,EventTriggerType type,UnityAction<BaseEventData> callBack)
    {
        EventTrigger eventTrigger = control.GetComponent<EventTrigger>();
        if (eventTrigger == null)
        {
            eventTrigger = control.gameObject.AddComponent<EventTrigger>();
        }
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = type;
        entry.callback.AddListener(callBack);

        eventTrigger.triggers.Add(entry);
    }
    
}
